#include "TcpServer.h"

TcpServer::TcpServer(std::string& ip,uint16_t& port)
{
    acceptor_ = new Acceptor(&loop_,ip,port);
    acceptor_->setnewConnectioncb(std::bind(&TcpServer::newConnection,this,std::placeholders::_1));
}

void TcpServer::start()
{
    loop_.run();
}
TcpServer::~TcpServer()
{
    delete acceptor_;
    for(auto & aa:conns_)
    {
        delete aa.second;
    }
}

void TcpServer::newConnection(Socket *clientsock)
{
    Connection* conn = new Connection(&loop_,clientsock);
    printf("accept client(fd=%d,ip=%s,port=%d) ok.\n", conn->fd(), conn->getiP().c_str(), conn->getport());

    conns_[conn->fd()] = conn;
}